<?php
class CsvController extends NodeController {
	
	static $csvService;
	
	public function i() {
		
		$rs = $this->isVerify ();
		if ($rs != self::SUCEESS) {
			$this->authorizeTemp ( $rs );
			return;
		}
		//存放uid
		$rs = self::getCSVService ()->stats ( $this->_user_global ['uid'] );
		
		$csv = self::getCSVService ()->getCSVByUid ( $this->_user_global ['uid'] );
		$topTen = self::getCSVService ()->getTopJsx ();
		$this->view ()->assign ( 'stats', $rs ); //当前累计订单数
		$this->view ()->assign ( 'top', $topTen );
		$this->view ()->assign ( 'csv', $csv );
		$this->view ()->assign ( 'user', $this->_user_global );
		$this->view ()->display ( "mysql:uploadify.tpl" );
	}
	
	public function statMan() {
		$rs = self::getCSVService ()->manStatus ();
		$this->view ()->assign ( 'data', $rs );
		$this->view ()->display ( "file:csv/man.tpl" );
	
	}
	
	public function del() {
		$rs = self::getCSVService ()->remove ( $_POST ['id'] );
		self::getLogService ()->add ( $this->_user_global ['real_name'], "删除订单录入$_POST[id]" );
		
		if ($rs > 0) {
			$this->sendNotice ( "删除成功", null, 1 );
		} else {
			$this->sendNotice ( "删除失败", null, 0 );
		}
	}
	
	public function export() {
		$this->isLogin ();
		self::getCSVService ()->export ( $this->_user_global ['uid'] );
	}
	
	public function exportAll() {
		$id = isset ( $_GET ['id'] ) ? trim ( $_GET ['id'] ) : 0;
		self::getCSVService ()->exportAll ( $id, $_GET ['type'] );
	
	}
	
	public function import() {
		self::getCSVService ()->confirm ();
	}
	
	public function search() {
		$rs = self::getCSVService ()->search ( $_GET ['key'], $_GET ['value'] );
		$this->view ()->assign ( 'news', $rs ['rs'] );
		$this->view ()->assign ( 'num', $rs ['page'] );
		$this->view ()->assign ( 'stat', $rs ['stat'] );
		$this->view ()->assign ( 'money', $rs ['money'] );
		$this->view ()->display ( 'file:csv/list.tpl' );
	}
	
	public function listing() {
		$rs = self::getCSVService ()->listing ( $_GET ['p'] );
		$stat = self::getCSVService ()->getOrderStat ();
		$money = self::getCSVService ()->getMoney ();
		//中奖单数
		$this->view ()->assign ( 'stat', $stat );
		$this->view ()->assign ( 'money', $money ['money'] );
		$this->view ()->assign ( 'news', $rs ['rs'] );
		$this->view ()->assign ( 'num', $rs ['page'] );
		
		$this->view ()->display ( 'file:csv/list.tpl' );
	}
	
	public function prize() {
		$rs = self::getCSVService ()->getPrize ( $_GET ['round'] );
		$this->view ()->assign ( 'prize', $rs ['prize'] );
		$this->view ()->assign ( 'user', $rs ['user'] );
		$this->view ()->assign ( 'hz', $rs ['hz'] );
		$this->view ()->assign ( 'number', $rs ['number'] );
		$this->view ()->assign ( 'money', $rs ['money'] );
		$this->view ()->display ( 'file:csv/prize.tpl' );
	}
	
	//数据统计
	public function stat() {
		
		$rs = self::getCSVService ()->stat ();
		$this->view ()->assign ( 'data', $rs );
		$this->view ()->display ( "file:csv/chart.tpl" );
	
	}
	
	public function upload() {
		
		// 验证码
		$member = new MemberService ();
		$user = $member->getMemberByUid ( $_POST ['uid'] );
		$flag = self::getCSVService ()->import ( $_POST ['timestamp'], $_POST ['token'], $user );
		
		if (is_numeric ( $flag )) {
			self::getLogService ()->add ( $user ['username'], "上传订单成功" );
			$this->sendNotice ( '上传成功 ,新增' . $flag . '单', null, true );
		} else {
			$this->sendNotice ( $flag, null, false );
			self::getLogService ()->add ( $user ['username'], "上传订单失败$flag" );
		
		}
	}
	
	public function log() {
		$rs = self::getCSVService ()->getAddLog ();
		$this->view ()->assign ( 'log', $rs );
		$this->view ()->display ( 'file:csv/addlog.tpl' );
	}
	
	public static function getCSVService() {
		if (self::$csvService == null) {
			self::$csvService = new CSVService ();
		}
		return self::$csvService;
	}
}